/**
 * @description 配置功能角色
 * @author 李艳
 */

import React, {Component} from 'react'
import connect from "react-redux/es/connect/connect";
import {show, hide} from '../../../configs/SharedReducer'
import TransferTable from "../../../commons/components/TransferTable";
import { message } from 'antd';
import {
  insertFeatureRoles, listAllAssignedFeatureRoles,
  listAllCanAssignFeatureRolesByRoleGroupAndUser,
  listAllCanAssignFeatureRolesByUser, removeFeatureRoles
} from "./EnterpriseUserService";
import {featureRoleGroupConfig} from "../../../configs/CommonComponentsConfig";

class ConfigFeatureRole extends Component {

  handleLeftClick = async (rows) => {
    const {userId} = this.props;
    let ids = [];
    for (let i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    await removeFeatureRoles({parentId: userId, childIds: ids}).then((data) => {
      const { status, message: msg } = data;
      if (status === 'SUCCESS') {
        message.success(msg);
      }
    }).catch(err => {
      this.props.hide()
    })

  }

  handleRightClick = async (rows) => {
    const {userId} = this.props;
    let ids = [];
    for (let i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    await insertFeatureRoles({parentId: userId, childIds: ids}).then((data) => {
      const { status, message: msg } = data;
      if (status === 'SUCCESS') {
        message.success(msg);
      }
    }).catch(err => {
      this.props.hide()
    })
  };

  leftService = async (params) => {
    let result = [];
    const {userId} = this.props;
    await listAllCanAssignFeatureRolesByUser({parentId: userId}).then((res) => {
      result = res;
    })
    return result;

  };

  rightService = async (params) => {
    let result = [];
    const {userId} = this.props;
    await listAllAssignedFeatureRoles({parentId: userId}).then((res) => {
      result = res
    });
    return result;
  };

  async JointQueryService(key) {
    let result = []
    const {userId} = this.props;
    let params = {};
    params.featureRoleGroupId = key;
    params.userId = userId;
    await listAllCanAssignFeatureRolesByRoleGroupAndUser(params).then((res) => {
      result = res
    });
    return result;
  };

  render() {
    const leftColumns = [
      {
        title: '角色代码',
        dataIndex: 'code',
        width: 200,
      },
      {
        title: '角色名称',
        dataIndex: 'name',
        width: 200,
      }
    ];
    const searchTableConfig = {...featureRoleGroupConfig, lable: "功能角色组"};
    return (

      <TransferTable
        style={{paddingRight: 8, paddingBottom: 8, paddingLeft: 8}}
        handleLeftClick={this.handleLeftClick}
        handleRightClick={this.handleRightClick}
        searchTableConfig={searchTableConfig}
        rightService={this.rightService.bind(this)}
        leftService={this.leftService.bind(this)}
        JointQueryService={this.JointQueryService.bind(this)}
        leftColumns={leftColumns}
        rightColumns={leftColumns}
        updateLeftByJointQueryService={true}
      />

    );
  }


}

const mapStateToProps = (state) => {
  return {};
}

const mapDispatchToProps = (dispatch) => {
  return {
    show: () => {
      dispatch(show())
    },
    hide: () => {
      dispatch(hide())
    }
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ConfigFeatureRole)


